package com.douwong.student.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.douwong.student.R;
import com.douwong.student.adapter.ScanIpAdapter;
import com.douwong.student.application.BaseApplication;
import com.douwong.student.base.BaseActivity;
import com.douwong.student.contant.Config;
import com.douwong.student.enums.E_CommandType;
import com.douwong.student.enums.E_Destination;
import com.douwong.student.model.LoginTeacherServiceInfo;
import com.douwong.student.model.TeacherOnline;
import com.douwong.student.socket.LTPServer;
import com.douwong.student.utils.UIUtils;

import java.util.List;

/**
 * @autor yichao
 * @time 2017/4/114:13
 * @des 加入课堂
 */
public class LoginClassActivity extends BaseActivity {
    private ListView scan_listview;
    private TextView scan_line_text, scan_line2_text, scan_cancel_text, scan_time_text;

    private List<TeacherOnline> mTeacherOnlineInfo;

    @Override
    protected void initView() {
        setContentView(R.layout.scan_ip_layout);
        scan_listview = (ListView) findViewById(R.id.scan_listview);
        scan_line_text = (TextView) findViewById(R.id.scan_line_text);
        scan_line2_text = (TextView) findViewById(R.id.scan_line2_text);
        scan_cancel_text = (TextView) findViewById(R.id.scan_cancel_text);
        scan_time_text = (TextView) findViewById(R.id.scan_time_text);
    }
    @Override
    protected void initData() {
        super.initData();
        scan_time_text.setText("在线老师的信息");
        String json = getIntent().getStringExtra("teacher_online_info");
        mTeacherOnlineInfo = JSON.parseArray(json, TeacherOnline.class);
        if (mTeacherOnlineInfo != null) {
            //    mTeacherOnlineList = JsonUtils.deserialize(teacherOnlineInfo, new ArrayList<TeacherOnline>().getClass());

            ScanIpAdapter scanIpAdapter = new ScanIpAdapter(this, mTeacherOnlineInfo);
            scan_listview.setAdapter(scanIpAdapter);
        }
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        scan_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //点击加入
                BaseApplication.temTeacherOnline = mTeacherOnlineInfo.get(position);
                LoginTeacherServiceInfo loginTeacherServiceInfo = new LoginTeacherServiceInfo();
                loginTeacherServiceInfo.setStudentInfo(BaseApplication.sStudentOnline);
                loginTeacherServiceInfo.setTeacherInfo(BaseApplication.temTeacherOnline);
                loginTeacherServiceInfo.studentAtt=BaseApplication.sStudentAtt;
                LTPServer
                        .createInstance(LoginClassActivity.this)
                        .sendMsg(loginTeacherServiceInfo, E_CommandType.serverLoginStudent, E_Destination.server,new int[]{0});
                finish();

            }
        });

        scan_cancel_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //取消
                UIUtils.sendBroadcast(LoginClassActivity.this, Config.CLOSE_LOGINING_POPUWINDOW);
                logcatI("点击取消");
                BaseApplication.isCancelConn = true;
                BaseApplication.isCloseConn=true;
                LTPServer.createInstance(LoginClassActivity.this).endWork();

                finish();
            }
        });
    }

    public static void startActivity(Context context, String json) {
        Intent intent = new Intent(context, LoginClassActivity.class);
        // intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("teacher_online_info", json);
        context.startActivity(intent);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        logcatI("关闭取消");
        BaseApplication.isCancelConn = false;

    }




}
